<template>
	<view class="wrap" v-if="itemList.length > 0">
		<view v-for="(item,index) in itemList" :key="index" @click="handleClick(item)" class="wrap_box" :style="{background: item.bg || '#FFF'}">
			<view class="wrap_box_img">
				<u--image :showLoading="true" :src="item.imgSrc" :width="item.imgWidth || '90rpx'" :height="item.imgHeight || '90rpx'">
				</u--image>
			</view>
			<view class="wrap_box_list" :style="item.tip ? 'padding-top: 30px' : 'padding-top: 40px'">
				<view class="wrap_box_list_title">{{item.title}}</view>
				<view v-if="item.tip" class="wrap_box_list_tip">
					{{item.tip}}
				</view>
			</view>
			<view class="wrap_box_icon" v-if="iconShow">
				<u-icon :name="item.iName || 'arrow-right'" :color="item.iColor" :size="item.iSize || 28"></u-icon>
			</view>
		</view>

		<view class="" @click="tologin()" style="text-align: center;color: #007AFF;">
			已有账号？去登陆
		</view>
	</view>
</template>

<script>
	/**
	 * itemList => []
	 * @@param {string} title  主标题  
	 * @@param {string} tip    副标题提示  
	 * @@param {string} imgSrc 图片路径  
	 * @@param {string} bg     当前卡片背景色  
	 * @@param {string} imgWidth   图片宽  默认90rpx 
	 * @@param {string} imgHeight  图片高  默认90rpx 
	 * @@param {string} iColor  icon颜色
	 * @@param {string} iName  icon名称  =》 默认角标 arrow-right
	 * @@param {string} iSize  icon大小  =》 默认28
	 * 
	 * @@param {string} iconShow  是否展示icon  默认展示
	 */
	export default {

		name: 'cellList',
		props: {
			itemList: {
				type: Array,
				default: () => [{
						name: 'resident',
						title: '我是员工',
						imgSrc: '/static/yg.png',
						tip: '我是员工 扫码加入企业',
						url: "/pages/myinfo/myinfo"
					},

					{
						name: 'worker',
						title: '我是老板/企业',
						imgSrc: '/static/qy.png',
						tip: '我是企业 填写信息创建企业',
						url: "/pages/bossregister/bossregister"
					},


				]
			},
			iconShow: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {

				openid: "",

			}
		},
		methods: {



			scanCode() {
				var that = this
				// 允许从相机和相册扫码
				uni.scanCode({
					scanType: ["qrCode"],
					success: (res) => {
						console.log(res);
						if (res.result) {
							const val = res.result;
							uni.navigateTo({
								url: "/pages/myinfo/myinfo?id=" + res.result
							})
						} else {

							console.log('请重新扫描');
							that.$refs.uToast.show({
								type: 'error',
								icon: false,
								title: '提示',
								message: "请重新扫描",
								iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
							})
							return false;
						}
					},
					fail: (res) => {
						that.$refs.uToast.show({
							type: 'error',
							icon: false,
							title: '提示',
							message: "未识别到二维码",
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
						})
						console.log('未识别到二维码');
					}
				})
			},
			tologin() {
				uni.navigateTo({
					url: "/pages/login/login",
				})
			},

			isRegister() {
				var that = this
				this.api.get("/User/isExistUserByOpenId/" + that.openid, {}).then(res => {
					console.log(res)
					that.$store.state.userId = res.data.userId
					that.$store.commit("getUserInfo")
				})
			},

			login() {
				var that = this
				uni.login({
					provider: 'weixin',
					success: function(res) {
						let appid = 'wx8b856c943358a30a'
						let secret = '5774d04dd7a10ed8887156053b04315f'
						let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' +
							res.code + '&grant_type=authorization_code';
						uni.request({
							url: url, // 请求路径
							success: result => {
								console.info(result.data.openid);
								that.openid = result.data.openid
								that.$store.state.openId = result.data.openid
								that.$store.commit("getUserInfo")
								that.isRegister()
							},
						});
					}
				});

			},
			handleClick(item) {
				if (item.name === "resident") {
					this.scanCode()
				} else {
					uni.navigateTo({
						url: item.url
					})
				}

				console.log(item)

			}
		},
		onLoad() {


		},
		onShow() {
			this.login()
			var that = this
			uni.getStorage({
				key: 'userInfo',
				success(res) {
					console.log('获取成功', res.data);
					const id =res.data.myCompanyId 
					if (res.data.myCompanyId  !== undefined &&res.data.myCompanyId!="") {
						console.log(that.$store.state.myCompanyId)
						uni.switchTab({
							url: '/pages/index/index'
						});
					}
				}
			})


			// if(this.$store.state.myCompanyId!=="undefined"){
			// 	console.log(this.$store.state.myCompanyId)
			//     	uni.switchTab({ url: '/pages/index/index' });
			// }
		}
	}
</script>

<style lang="scss" scoped>
	.wrap {
		padding: 0 15px;
		padding-top: 60rpx;

		&_box {
			display: flex;
			margin-bottom: 60rpx;
			height: 210rpx;
			box-shadow: 0px 0px 17px 0px rgba(99, 147, 255, 0.1000);
			border-radius: 40rpx;

			&_img {
				display: flex;
				flex-direction: row;
				align-items: center;
				margin: 0 30rpx;
			}

			&_list {
				flex: 1;

				&_title {
					font-weight: 600;
					font-size: 16px;
					color: #333333;
					padding-bottom: 14rpx;
				}

				&_tip {
					font-size: 14px;
					font-weight: 400;
					color: #999999;
				}
			}

			&_icon {
				width: 80rpx;
				display: flex;
				flex-direction: row;
				align-items: center;
				padding-left: 40rpx;
			}
		}
	}
</style>
